package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalCooperationReductionTimeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.exception.BssException;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月31日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalCooperationReductionTimeAttrHandler.class)
public class LocalCooperationReductionTimeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        // 重提初始化不校验
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (Objects.nonNull(extParams)&& extParams.containsKey("nodeCode")) {
            return;
        }
        if (Objects.isNull(attr)) {
            return;
        }
        if (KiteStringUtils.isNotEmpty(attr.getValue())) {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = sdf.format(d);
            //合作减免时间&测试截止时间 只能选择当天之后的日期
            if (!Objects.equals(1, KiteDateUtils.compareDate(attr.getValue(), nowDate, KiteDateUtils.DATE_FORMAT))){
                BssException.throwOut("", "",  "只能选择当天之后的日期，请重新选择！");
            }
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
